package com.itheima.web.controller.cargo;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.domain.cargo.Export;
import com.itheima.domain.cargo.ExportProduct;
import com.itheima.domain.cargo.ExportProductExample;
import com.itheima.domain.system.User;
import com.itheima.service.cargo.ExportProductService;
import com.itheima.service.cargo.ExportService;
import com.itheima.web.controller.BaseController;
import com.itheima.web.utils.BeanMapUtils;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.util.*;

@Controller
@RequestMapping("/cargo/export")
public class PdfController extends BaseController {

    @Autowired
    private DataSource dataSource;
    @Reference
    private ExportService exportService;
    @Reference
    private ExportProductService exportProductService;

    @RequestMapping("/exportPdf_1")
    public void exportPdf_1() throws Exception {
        //加载jasper文件，获取文件流
        InputStream in = session.getServletContext().getResourceAsStream("/jasper/test1_jdbc.jasper");
        //获取数据库连接
        Connection connection = dataSource.getConnection();
        //创建JasperPrint对象
        //参数1:模板文件输入流；参数2：传递到模板文件中的key-value类型的参数；参数3：数据列表参数
        JasperPrint jasperPrint = JasperFillManager.fillReport(in, new HashMap<>(), connection);
        //3. 以pdf形式输出
        JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());
    }

    @RequestMapping("/exportPdf_2")
    public void exportPdf_2() throws Exception {
        //加载jasper文件，获取文件流
        InputStream in = session.getServletContext().getResourceAsStream("/jasper/test2_javaBean.jasper");
        //2. 创建JasperPrint对象
        List<User> list = new ArrayList<>();
        for(int i=0;i<10;i++) {
            User user = new User();
            user.setUserName("name"+i);
            user.setEmail(i+"@qq.com");
            user.setCompanyName("企业"+i);
            user.setDeptName("部门"+i);
            list.add(user);
        }
        JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(list);
        //参数1:模板文件输入流；参数2：传递到模板文件中的key-value类型的参数；参数3：数据列表参数
        JasperPrint jasperPrint = JasperFillManager.fillReport(in, new HashMap<>(), dataSource);
        //以pdf形式输出
        JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());
    }

    @RequestMapping("/exportPdf_3")
    public void exportPdf_3() throws Exception {
        //1. 加载jasper文件，获取文件流
        InputStream in =
                session.getServletContext().getResourceAsStream("/jasper/test1_group.jasper");

        //2. 创建JasperPrint对象
        List<User> list = new ArrayList<>();
        for(int i=0;i<10;i++) {
            for(int j=0;j<5;j++) {
                User user = new User();
                user.setUserName("name"+j);
                user.setEmail(j+"@qq.com");
                user.setCompanyName("企业"+i);
                user.setDeptName("部门"+j);
                list.add(user);
            }
        }
        //将list构造为jrdatasource
        JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(list);

        //参数1:模板文件输入流；参数2：传递到模板文件中的key-value类型的参数；参数3：数据列表参数
        JasperPrint jasperPrint =
                JasperFillManager.fillReport(in,new HashMap<>(),dataSource);
        //3. 以pdf形式输出
        JasperExportManager.
                exportReportToPdfStream(jasperPrint,response.getOutputStream());
    }

    //@RequestMapping("/exportPdf")
    public void exportPdf_4() throws Exception {
        //1. 加载jasper文件，获取文件流
        InputStream in =
                session.getServletContext().getResourceAsStream("/jasper/test2_chart.jasper");

        //2. 创建JasperPrint对象
        //构造数据列表的数据源对象
        List list = new ArrayList();
        for(int i=0;i<6;i++) {
            Map map = new HashMap();
            map.put("title","标题"+i);
            map.put("value",new Random().nextInt(100));
            list.add(map);
        }
        //将list构造为jrdatasource
        JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(list);

        //参数1:模板文件输入流；参数2：传递到模板文件中的key-value类型的参数；参数3：数据列表参数
        JasperPrint jasperPrint =
                JasperFillManager.fillReport(in,new HashMap<>(),dataSource);
        //3. 以pdf形式输出
        JasperExportManager.
                exportReportToPdfStream(jasperPrint,response.getOutputStream());
    }

    @RequestMapping("/exportPdf")
    public void exportPdf(String id) throws Exception {
        //1. 加载jasper文件，获取文件流
        InputStream in =
                session.getServletContext().getResourceAsStream("/jasper/export.jasper");

        //2. 创建JasperPrint对象
        //构造模板数据
        //a.根据id查询报运单，将对象转化为map集合
        Export export = exportService.findById(id);
        Map<String, Object> map = BeanMapUtils.beanToMap(export);
        //b.根据报运单id，查询此报运单下的所有货物
        ExportProductExample example = new ExportProductExample();
        ExportProductExample.Criteria criteria = example.createCriteria();
        criteria.andExportIdEqualTo(id);
        List<ExportProduct> list = exportProductService.findAll(example);

        //将list构造为jrdatasource
        JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(list);

        //参数1:模板文件输入流；参数2：传递到模板文件中的key-value类型的参数；参数3：数据列表参数
        JasperPrint jasperPrint =
                JasperFillManager.fillReport(in,map,dataSource);
        //3. 以pdf形式输出
        JasperExportManager.
                exportReportToPdfStream(jasperPrint,response.getOutputStream());
    }

}
